package com.cxf.windex.rest;

import com.cxf.windex.base.rest.BaseRestController;
import com.cxf.windex.base.util.TreeUtil;
import com.cxf.windex.base.vo.MenuTree;
import com.cxf.windex.biz.WeiMenuBiz;
import com.cxf.windex.entity.WeiMenu;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * @author chenxf
 * @date 4/10/2018
 * @description
 */
@Api(tags = {"内盘 - 菜单 接口"})
@Controller
@RequestMapping("menu")
public class WeiMenuController extends BaseRestController<WeiMenuBiz,WeiMenu> {
  @ApiOperation(value = "查询菜单 ", notes = "查询菜单 ")
  @RequestMapping(value = "/initMenu", method = RequestMethod.GET)
  @ResponseBody
  public List<MenuTree> menu() {

    String type = this.getCurrentUserType();


    Example example = new Example(WeiMenu.class);
    Criteria criteria = example.createCriteria();

    if (StringUtils.isNotBlank(type)) {
      if(Integer.valueOf(type)<4){
        criteria.andLessThanOrEqualTo("type",  type );
      }else {
        criteria.andEqualTo("type",  type );
      }
    }
    example.setOrderByClause(" order_num asc");
    List<WeiMenu> menus=baseBiz.selectByExample(example);
    List<MenuTree> menuTrees=getMenuTree(menus,0L);
   return menuTrees;


  }

  @ApiOperation(value = "查询菜单 ", notes = "查询菜单 ")
  @RequestMapping(value = "/list", method = RequestMethod.GET)
  @ResponseBody
  public List<WeiMenu> list1() {

    String type = this.getCurrentUserType();


    Example example = new Example(WeiMenu.class);
    Criteria criteria = example.createCriteria();

    if (StringUtils.isNotBlank(type)) {
      criteria.andLessThanOrEqualTo("type",  type );
    }
    example.setOrderByClause(" order_num asc");
    return baseBiz.selectByExample(example);


  }

  private List<MenuTree> getMenuTree(List<WeiMenu> menus,long root) {
    List<MenuTree> trees = new ArrayList<MenuTree>();
    MenuTree node = null;
    for (WeiMenu menu : menus) {
      node = new MenuTree();
      BeanUtils.copyProperties(menu, node);
      trees.add(node);
    }
    return TreeUtil.buildByRecursive(trees,root) ;
  }
}
